﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Model;
using Ledict.Utility.Common;
using Ledict.Utility.Msg;

namespace Ledict.ConditionExtract.Frm.NodeTree
{
    /// <summary>
    /// 要素关系配置界面，根据CC_ID
    /// </summary>
    public partial class FrmSuitTypeRelConfig : BaseForm
    {
        readonly XmlLabel_SuitTypeRelBLL _xmlLabelSuitTypeRelBll = new XmlLabel_SuitTypeRelBLL();

        /// <summary>
        /// 页面标签与诉情种类关系列表
        /// </summary>
        public IList<SYS_XmlLabel_SuitTypeRel> SysXmlLabelSuitTypeRels { get; set; }
        /// <summary>
        /// 情形情节ID
        /// </summary>
        public string CcId { get; set; }


        public FrmSuitTypeRelConfig()
        {
            InitializeComponent();

        }

        /// <summary>
        /// 界面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmSuitTypeRelConfig_Load(object sender, EventArgs e)
        {
            BindData(CcId);
        }

        /// <summary>
        /// 通过CCID获取诉请要素关系配置信息
        /// </summary>
        /// <param name="ccId">CCID</param>
        private void BindData(string ccId)
        {
            var dt = _xmlLabelSuitTypeRelBll.GetSuitTypeRelListByCcid(ccId);
            dgvSuitTypeRel.AutoGenerateColumns = false;
            dgvSuitTypeRel.DataSource = dt;
        }

        /// <summary>
        /// 生成顺序号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSuitTypeRel_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            var rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dgvSuitTypeRel.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dgvSuitTypeRel.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dgvSuitTypeRel.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }


        #region 编辑
        /// <summary>
        /// 开始编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSuitTypeRel_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {        
            if (e.ColumnIndex != 5)
                return;
        }

        /// <summary>
        /// 结束编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSuitTypeRel_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //修改显示顺序
            if (e.ColumnIndex == 5)
            {
                //var index = e.RowIndex;
                var showIndex = dgvSuitTypeRel.Rows[e.RowIndex].Cells["ShowIndex"].Value.ToString();
                var xsrId = dgvSuitTypeRel.Rows[e.RowIndex].Cells["XSR_ID"].Value.ToString();

                if (_xmlLabelSuitTypeRelBll.UpdateIndex(xsrId, string.IsNullOrEmpty(showIndex.Trim()) ? "null" : showIndex) <= 0)
                {
                    MsgBox.Err(ConventionConst.UpdateFailMsg);
                }
            }

            //修改标签路径
            if (e.ColumnIndex == 3)
            {
                var xPath = dgvSuitTypeRel.Rows[e.RowIndex].Cells["LabelXpath"].Value.ToString();
                var xsrId = dgvSuitTypeRel.Rows[e.RowIndex].Cells["XSR_ID"].Value.ToString();

                if (_xmlLabelSuitTypeRelBll.UpdateXPath(xsrId, xPath) <= 0)
                {
                    MsgBox.Err(ConventionConst.UpdateFailMsg);
                }
            }

        }

        /// <summary>
        /// 修改是否显示标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSuitTypeRel_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取checkbox的值（是否显示标签）
            if (e.ColumnIndex == 4 && e.RowIndex != -1)
            {
                //获取控件的值
                var isShowLabel = dgvSuitTypeRel.Rows[e.RowIndex].Cells["IsShowLableDisplay"].EditedFormattedValue.ToString();
                var xsrId = dgvSuitTypeRel.Rows[e.RowIndex].Cells["XSR_ID"].Value.ToString();

                if (_xmlLabelSuitTypeRelBll.UpdateIsShowLabel(xsrId, isShowLabel.ToLower().Equals("true")) <= 0)
                {
                    MsgBox.Err(ConventionConst.UpdateFailMsg);
                }
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除，支持批量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //获取选中的行
            if (dgvSuitTypeRel.SelectedRows.Count > 0)
            {
                var dialogResult = MsgBox.Ask("删除后无法恢复，确认继续操作？");
                if (dialogResult == DialogResult.Yes)
                {
                    var deleteList = (from DataGridViewRow selectedRow in dgvSuitTypeRel.SelectedRows select selectedRow.Cells["XSR_ID"].Value.ToString()).ToList();
                    if (_xmlLabelSuitTypeRelBll.DeleteBatchByXSR_ID(deleteList) > 0)
                    {
                        MsgBox.OK("删除成功");
                        BindData(CcId);
                    }
                }
            }
            else
            {
                MsgBox.Warn("未选择任何行");
            }

        }
        #endregion

        #region 修改顺序
        /// <summary>
        /// 修改顺序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOrderEdit_Click(object sender, EventArgs e)
        {
            var frm = new FrmSuitTypeRelIndexConfig() { CcId = CcId };
            var result = frm.ShowDialog();
            if (result == DialogResult.OK)
                BindData(CcId);
        }





        #endregion

        private void dgvSuitTypeRel_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            //修改显示顺序
            if (e.ColumnIndex == 5)
            {
                //var index = e.RowIndex;
                var showIndex = dgvSuitTypeRel.Rows[e.RowIndex].Cells["ShowIndex"].Value.ToString();
                var xsrId = dgvSuitTypeRel.Rows[e.RowIndex].Cells["XSR_ID"].Value.ToString();

                if (_xmlLabelSuitTypeRelBll.UpdateIndex(xsrId, string.IsNullOrEmpty(showIndex.Trim()) ? "null" : showIndex) <= 0)
                {
                    MsgBox.Err(ConventionConst.UpdateFailMsg);
                }
            }

            //修改标签路径
            if (e.ColumnIndex == 3)
            {
                var xPath = dgvSuitTypeRel.Rows[e.RowIndex].Cells["LabelXpath"].Value.ToString();
                var xsrId = dgvSuitTypeRel.Rows[e.RowIndex].Cells["XSR_ID"].Value.ToString();

                if (_xmlLabelSuitTypeRelBll.UpdateXPath(xsrId, xPath) <= 0)
                {
                    MsgBox.Err(ConventionConst.UpdateFailMsg);
                }
            }
        }

        /// <summary>
        /// 保存关系
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgvSuitTypeRel.Rows.Count-1; i++)
            {
                var showIndex = dgvSuitTypeRel.Rows[i].Cells["ShowIndex"].Value.ToString();
                var xsrId = dgvSuitTypeRel.Rows[i].Cells["XSR_ID"].Value.ToString();
                var xPath = dgvSuitTypeRel.Rows[i].Cells["LabelXpath"].Value.ToString();
                if (_xmlLabelSuitTypeRelBll.UpdateIndexAndXPath(xsrId, xPath, showIndex) <= 0)
                {
                    MsgBox.Err(ConventionConst.UpdateFailMsg);
                }
            }
            MessageBox.Show("保存成功！");
        }
    }
}
